const BigNumber = require('bignumber.js');
const moment = require('moment');
const { think } = require('thinkjs');

module.exports = class extends think.Model {
  async goumai(type, domain, user, ip) {
    const rate = await this.model('set').where({id: 4}).getField('percent1', true);
    const orderPrice = BigNumber(domain.price).multipliedBy(BigNumber(rate)).multipliedBy(BigNumber(domain.days)).dividedBy(360).toNumber().toFixed(2);

    const orderSn = domain.order_no + moment().format('DDHHmmss');

    const orderInfo = {
      domain_id: domain.id,
      order_no: orderSn,
      user_id: user,
      create_time: ['exp', 'CURRENT_TIMESTAMP()'],
      total_price: orderPrice,
      month: moment().format('YYYY-MM'),
      method: type,
      pay_status: 1
    };

    await this.model('order_pay').add(orderInfo);

    const rPrice = new BigNumber(orderPrice).multipliedBy(new BigNumber(100)).toNumber();

    if (type === '2') {
      const WeixinSerivce = think.service('wxpay', 'api');
      try {
        const returnParams = await WeixinSerivce.wxappletPay({
          body: '订单编号：' + orderSn,
          out_trade_no: orderSn,
          // total_fee: '' + rPrice,
          total_fee: '1',
          spbill_create_ip: ip
        });
        return returnParams;
      } catch (err) {
        console.log(err, '微信支付失败');
        return null;
      }
    } else if (type === '1') {
      const WeixinSerivce = think.service('alipay', 'api');
      try {
        const returnParams = await WeixinSerivce.wxappletPay({
          body: '订单编号：' + orderSn,
          out_trade_no: orderSn,
          // total_fee: '' + orderPrice
          total_fee: '0.01'
        });

        return returnParams;
      } catch (err) {
        console.log(err, '支付宝支付失败');
        return null;
      }
    }
  }

  async yanqi(type, domain, user, ip) {
    const rate = await this.model('set').where({id: 4}).getField('percent1', true);
    const order = await this.model('order').where({domain_id: domain.id, status: 42}).find();
    const orderPrice = BigNumber(domain.price).multipliedBy(BigNumber(rate)).multipliedBy(BigNumber(order.days)).dividedBy(360).toNumber().toFixed(2);

    const orderSn = domain.order_no + moment().format('DDHHmmss');

    const orderInfo = {
      domain_id: domain.id,
      order_no: orderSn,
      user_id: user,
      create_time: ['exp', 'CURRENT_TIMESTAMP()'],
      total_price: orderPrice,
      month: moment().format('YYYY-MM'),
      method: type,
      pay_status: 2
    };

    await this.model('order_pay').add(orderInfo);

    const rPrice = new BigNumber(orderPrice).multipliedBy(new BigNumber(100)).toNumber();

    if (type === '2') {
      const WeixinSerivce = think.service('wxpay', 'api');
      try {
        const returnParams = await WeixinSerivce.wxappletPay({
          body: '订单编号：' + orderSn,
          out_trade_no: orderSn,
          // total_fee: '' + rPrice,
          total_fee: '1',
          spbill_create_ip: ip
        });
        return returnParams;
      } catch (err) {
        console.log(err, '微信支付失败');
        return null;
      }
    } else if (type === '1') {
      const WeixinSerivce = think.service('alipay', 'api');
      try {
        const returnParams = await WeixinSerivce.wxappletPay({
          body: '订单编号：' + orderSn,
          out_trade_no: orderSn,
          // total_fee: '' + orderPrice
          total_fee: '0.01'
        });

        return this.success(returnParams);
      } catch (err) {
        console.log(err, '支付宝支付失败');
        return this.fail('支付宝支付失败');
      }
    }
  }
};
